package com.wkk.nursinghome.controller;

import com.wkk.nursinghome.bean.ResultBean;
import com.wkk.nursinghome.domain.Employee;
import com.wkk.nursinghome.service.IEmployeeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class EmployeeController {
    //注入Service对象
    @Resource
    private IEmployeeService employeeService;

    //显示所有信息
    @GetMapping("/employee")
    public String selectAll(Model model){
        //获取所有信息
        List<Employee> employees=employeeService.selectAll();
        //将获取的信息添加到model中
        model.addAttribute("emps",employees);
        //设置返回的试图名称-->templates/employee.html
        return "employee";
    }

    //删除员工
    @DeleteMapping("/employee/{id}")
    @ResponseBody
    public String deleteById(@PathVariable Integer id){
        System.out.println("即将要删除的编号："+id);
        //调用方法判断是否删除成功
        boolean idDeleteSuccess=employeeService.deleteEmployeeById(id);
        if(idDeleteSuccess){
            return "success";
        }else {
            return "fail";
        }
    }
    /**
     * 显示修改信息页面
     * @return
     */
    @GetMapping("/employee/{id}")
    public String getEmployeeById(@PathVariable("id")Integer id, Model model) {
        Employee employee = employeeService.queryById(id);
        System.out.println(employee);
        model.addAttribute("emp", employee);
        return "updateEmployee";
    }

    //修改部门信息
    @PutMapping("/employee")
    public String update(Employee employee){
        boolean ismodifysuccess=employeeService.updateEmployee(employee);
        if(ismodifysuccess) {
            return "redirect:/employee";
        }else{
            return "first";
        }
    }

    //显示新增部门信息页面
    @GetMapping("/addEmp")
    public String addDept(){
        return "/addEmployee";
    }
    //增加部门信息
    @PutMapping("/addE")
    public String add(Employee employee){
        employeeService.addEmployee(employee);
        return "redirect:/employee";
    }

    //根据条件查询
    @GetMapping("/searchE")
    public String searchByCondition(Model model,Employee employee){
        List<Employee> employees= employeeService.selectByCondition(employee);
        model.addAttribute("emps",employees);
        return "/employee";
    }

    //显示登录页面
    @GetMapping("/login")
    public String login(){
        return "/login";
    }

    //登录
    @RequestMapping(value = "checklogin" ,method = RequestMethod.POST)
    @ResponseBody
    public ResultBean checklogin(Integer id, String name, HttpSession session)  {
        ResultBean resultBean=new ResultBean(200,"登陆成功",true);
        boolean flag=employeeService.login(id, name);
        System.out.println(flag);
        if (flag) {
            //登录查询，获取用户详细信息
            Employee employee=new Employee();
            employee.setName(name);
            resultBean.setData(employee);
            //保存至session中
            session.setAttribute("USER", resultBean);
        }else {
            resultBean=new ResultBean(200,"登陆失败",false);
            session.removeAttribute("USER");
        }
        return resultBean;
    }
}
